import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store'
Vue.use(Router)
Vue.use(store)
const router = new Router({

  routes: [

    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => require(['@/components/Login/Login'], resolve),
      meta: {
        needLogin: false,
        title: '登录',
        // keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/home',
      name: 'home',
      component: resolve => require(['@/components/home/homeIndex'], resolve),
      meta: {
        needLogin: true,
        title: '首页',
      }
    },

    {
      path: '/setPwd',
      component: resolve => require(['@/components/Login/setPwd'], resolve),
      meta: {
        title: '设置密码',
        // keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/fogotPwd',
      component: resolve => require(['@/components/Login/fogotPwd'], resolve),
      meta: {
        title: '忘记密码',
        // keepAlive: true // 需要被缓存
      }
    },

    {
      path: '/attendanceIndex',
      component: resolve => require(['@/components/attendance/attendance'], resolve),
      meta: {
        title: '考勤打卡',
      }
    },
    {
      path: '/checkIndex',
      component: resolve => require(['@/components/check/checkIndexList'], resolve),
      meta: {
        title: '考核',
      }
    },
    {
      path: '/approveTem',
      component: resolve => require(['@/components/approve/approveTem'], resolve),
      meta: {
        title: '审批模板',
      }
    },
    {
      path: '/attendanceApply',
      component: resolve => require(['@/components/approve/attendanceApply'], resolve),
      meta: {
        title: '审批请假',
      }
    },
    {
      path: '/approveIndex',
      component: resolve => require(['@/components/approve/approveIndex'], resolve),
      meta: {
        title: '审批首页',
      }
    },
    {
      path: '/attendanceApplyPass',
      component: resolve => require(['@/components/approve/attendanceApplyPass'], resolve),
      meta: {
        title: '审批记录详情',
      }
    },
    {
      path: '/needapproveApplyPass',
      component: resolve => require(['@/components/approve/needapproveApplyPass'], resolve),
      meta: {
        title: '我的审批记录详情',
      }
    },
    {
      path: '/copyTomePass',
      component: resolve => require(['@/components/approve/copyTomePass'], resolve),
      meta: {
        title: '抄送我的详情',
      }
    },
    {
      path: '/approveComment',
      component: resolve => require(['@/components/approve/approveComment'], resolve),
      meta: {
        title: '评论',
      }
    },

    {
      path: '/cleanerList',
      component: resolve => require(['@/components/check/clearn/cleanerList'], resolve),
      meta: {
        title: '保洁员列表',
      }
    },
    {
      path: '/checkList',
      component: resolve => require(['@/components/check/clearn/checkList'], resolve),
      meta: {
        title: '保洁员考核记录',
      }
    },

    {
      path: '/cleanerCheckUpdate',
      component: resolve => require(['@/components/check/clearn/cleanerCheckUpdate'], resolve),
      meta: {
        title: '修改保洁员考核',
      }
    },
    {
      path: '/cleanerCheck',
      component: resolve => require(['@/components/check/clearn/cleanerCheck'], resolve),
      meta: {
        title: '保洁员考核',
      }
    },
    {
      path: '/managerList',
      component: resolve => require(['@/components/check/manager/managerList'], resolve),
      meta: {
        title: '管理员列表',
      }
    },
    {
      path: '/managerCheckUpdate',
      component: resolve => require(['@/components/check/manager/managerCheckUpdate'], resolve),
      meta: {
        title: '修改管理员考核',
      }
    },
    {
      path: '/managerCheck',
      component: resolve => require(['@/components/check/manager/managerCheck'], resolve),
      meta: {
        title: '管理员考核',
      }
    },
    {
      path: '/projectList',
      component: resolve => require(['@/components/check/project/projectList'], resolve),
      meta: {
        title: '项目部列表',
      }
    },
    {
      path: '/projectCheckUpdate',
      component: resolve => require(['@/components/check/project/projectCheckUpdate'], resolve),
      meta: {
        title: '修改项目部考核',
      }
    },
    {
      path: '/projectCheck',
      component: resolve => require(['@/components/check/project/projectCheck'], resolve),
      meta: {
        title: '项目部考核',
      }
    },
    {
      path: '/projectCompanyList',
      component: resolve => require(['@/components/check/projectCompany/projectCompanyList'], resolve),
      meta: {
        title: '项目公司列表',
      }
    },
    {
      path: '/projectCompanyCheckUpdate',
      component: resolve => require(['@/components/check/projectCompany/projectCompanyCheckUpdate'], resolve),
      meta: {
        title: '修改项目公司考核',
      }
    },
    {
      path: '/projectCompanyCheck',
      component: resolve => require(['@/components/check/projectCompany/projectCompanyCheck'], resolve),
      meta: {
        title: '项目公司考核',
      }
    },

    {
      path: '/deviceCheckUpdate',
      component: resolve => require(['@/components/check/device/deviceCheckUpdate'], resolve),
      meta: {
        title: '修改设备考核',
      }
    },
    {
      path: '/deviceCheck',
      component: resolve => require(['@/components/check/device/deviceCheck'], resolve),
      meta: {
        title: '设备考核',
      }
    },
    {
      path: '/deviceList',
      component: resolve => require(['@/components/check/device/deviceList'], resolve),
      meta: {
        title: '设备考核列表',
      }
    },
    {
      path: '/administrativeCheckUpdate',
      component: resolve => require(['@/components/check/administrative/administrativeCheckUpdate'], resolve),
      meta: {
        title: '修改行政考核',
      }
    },
    {
      path: '/administrativeCheck',
      component: resolve => require(['@/components/check/administrative/administrativeCheck'], resolve),
      meta: {
        title: '行政考核',
      }
    },
    {
      path: '/administrativeList',
      component: resolve => require(['@/components/check/administrative/administrativeList'], resolve),
      meta: {
        title: '行政考核列表',
      }
    },
    {
      path: '/financeCheckUpdate',
      component: resolve => require(['@/components/check/finance/financeCheckUpdate'], resolve),
      meta: {
        title: '修改财务考核',
      }
    },
    {
      path: '/financeCheck',
      component: resolve => require(['@/components/check/finance/financeCheck'], resolve),
      meta: {
        title: '财务考核',
      }
    },
    {
      path: '/financeList',
      component: resolve => require(['@/components/check/finance/financeList'], resolve),
      meta: {
        title: '财务考核列表',
      }
    },
    {
      path: '/profitCheckUpdate',
      component: resolve => require(['@/components/check/profit/profitCheckUpdate'], resolve),
      meta: {
        title: '修改利润考核',
      }
    },
    {
      path: '/profitList',
      component: resolve => require(['@/components/check/profit/profitList'], resolve),
      meta: {
        title: '利润考核列表',
      }
    },
    {
      path: '/profitCheck',
      component: resolve => require(['@/components/check/profit/profitCheck'], resolve),
      meta: {
        title: '利润考核',
      }
    },
    {
      path: '/setNewPwd',
      component: resolve => require(['@/components/home/setNewPwd'], resolve),
      meta: {
        title: '修改密码',
      }
    },
    {
      path: '/complainForm',
      component: resolve => require(['@/components/home/complainForm'], resolve),
      meta: {
        title: '投诉建议表单',
      }
    },

    {
      path: '/warnMessage',
      component: resolve => require(['@/components/warnMessage/warnIndex'], resolve),
      meta: {
        title: '报警信息',
      }
    },
    {
      path: '/monitorIndex',
      component: resolve => require(['@/components/monitor/mapDisplay'], resolve),
      meta: {
        title: '实时监控',
      }
    },
    {
      path: '/monitortrack',
      component: resolve => require(['@/components/monitor/track'], resolve),
      meta: {
        title: '历史轨迹',
      }
    },
    {
      path: '/attendancecheck',
      component: resolve => require(['@/components/attendance/attendancecheck'], resolve),
      meta: {
        title: '考勤巡查',
      }
    },
    {
      path: '/attendanceScavenging',
      component: resolve => require(['@/components/attendance/attendanceScavenging'], resolve),
      meta: {
        title: '垃圾清运',
      }
    },

    {
      path: '/communicateIndex',
      component: resolve => require(['@/components/communicate/communicateIndex'], resolve),
      meta: {
        title: '实时通信',
      }
    },
    {
      path: '/communicateList',
      component: resolve => require(['@/components/communicate/communicateList'], resolve),
      meta: {
        title: '实时对讲',
      }
    },


  ]
})

/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

//全局路由守卫
router.beforeEach((to, from, next) => {
  console.log('navigation-guards');
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
  let isLogin = localStorage.getItem('hasLogin'); // 是否登录
  console.log("状态", isLogin)
  const nextRoute = ['home', 'warnMessage'];

  // 未登录状态；当路由到nextRoute指定页时，跳转至login
  if (nextRoute.indexOf(to.name) >= 0) {
    if (isLogin == 'false') {
      router.push({ name: 'login' })
    }
  }
  // 已登录状态；当路由到login时，跳转至home 
  if (to.name === 'login') {
    console.log(111)
    if (isLogin == 'true') {
      console.log(122)
      router.push({ name: 'home' });
    }
  }
  next();
});
export default router;